package main

import (
	"context"
	"fmt"
	"github.com/mholt/archiver/v4"
	"os"
)

func main() {
	// map files on disk to their paths in the archive
	files, err := archiver.FilesFromDisk(nil, map[string]string{
		"/root/GolandProjects/godemo/file1.txt": "file1.txt",
		"/root/GolandProjects/godemo/file2.txt": "subfolder/file2.txt",
		//"~/GolandProjects/godemo/file3.txt": "",             // put in root of archive as file3.txt
		"/root/GolandProjects/godemo/file4.txt":    "subfolder/",   // put in subfolder as file4.txt
		"/root/GolandProjects/godemo/tests/folder": "CustomFolder", // contents added recursively
	})
	if err != nil {
		fmt.Println(err)
	}

	// create the output file we'll write to
	out, err := os.Create("example.tar.gz")
	if err != nil {
		fmt.Println(err)
	}
	//defer out.

	// we can use the CompressedArchive type to gzip a tarball
	// (compression is not required; you could use Tar directly)
	format := archiver.CompressedArchive{
		Compression: archiver.Gz{},
		Archival:    archiver.Tar{},
	}

	// create the archive
	err = format.Archive(context.Background(), out, files)
	if err != nil {
		fmt.Println(err)
	}
}
